package cn.com.generaldata.os.ws.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cn.com.generaldata.os.util.CacheManager;
import cn.com.generaldata.os.util.ReadXmlUtil;
import cn.com.generaldata.os.vo.Menu;


@Component
@InjectName
public class HyjjhsAction {
	
	@Value("${bi.ip}")
	private String ip;
	
	@Value("${busername}")
	private String busername;
	
	@Value("${bi.servlet.addr}")
	private String addr;
	
	
	@Value("${file3}")
	private String parma3;
	
	@Value("${appendFile}")
	private String appendFile;
	
	@Value("${rootElement}")
	private String rootElement;

	String uid = "1";

	@At("/hyjjhs")
    @Ok("json")
    public Menu getHyjjhsXml() {
		List<Menu> menuList = null;
		Menu sjfx3 = null;

		try {
			//提取菜单
			URL url = new URL("http://"+ip+addr + "&busername=" + busername);
			HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
			urlConn.setDoOutput(true);
			urlConn.setDoInput(true);
			urlConn.setUseCaches(false);
//			urlConn.setRequestProperty("Content-type","application/x-java-serialized-object");
//			urlConn.setRequestProperty("Content-type","text/html");
			urlConn.setRequestMethod("GET");
			urlConn.connect();
			InputStream inStrm = urlConn.getInputStream();
			int BUFFER_SIZE = 1024;
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
	        byte[] data = new byte[BUFFER_SIZE];  
	        int count = -1;  
	        while((count = inStrm.read(data,0,BUFFER_SIZE)) != -1)  
	            outStream.write(data, 0, count);  
	          
	        data = null;  
	        String xml = new String(outStream.toByteArray(),"utf-8"); 

	        rootElement = "江苏海洋经济";
	        parma3 = "海洋经济核算";
	        
	        menuList = ReadXmlUtil.getListByXml(xml, busername, appendFile, ip, rootElement);
	        
	        for (Menu s : menuList) {
				if (s.getText().equals(parma3)) {//海洋经济核算--直接读取bi图形
				    sjfx3 = s;
					break;
				}
			}
        
	        inStrm.close();
	        outStream.close();
		} catch (MalformedURLException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
        return sjfx3;
    }
	
	
	

}
